package com.tjl.nianhuiruchang;

import com.tjl.synchronized_practice.Ticket;

import java.util.concurrent.locks.ReentrantLock;

public class Test {
    public static void main(String[] args) {
//        1、自定义类实现Runnable接口，定义变量int number = 100;模拟100个人。
        People people = new People();
//        2、定义测试类，启动两个线程分别模拟：前门、后门。
        Task task1 = new Task(people);
        Task task2 = new Task(people);
        Thread qianmen = new Thread(task1, "前门");
        Thread houmen = new Thread(task2, "后门");
        qianmen.start();
        houmen.start();


    }
}


class Task implements Runnable {

    private final int number = 100;
    private People people; //公共资源

    public Task(People people) {
        this.people = people;
    }

    @Override
    public void run() {
        while ( people.getInto() < number) {
            people.goin();
        }
    }
}


class People {

    private int into = 0;
    ReentrantLock lock = new ReentrantLock();

    public int getInto() {
        return into;
    }

    public void setInto(int into) {
        this.into = into;
    }

    public  void goin() {

        Thread thread = Thread.currentThread();
        String numbers = DoubleColorBallUtil.create();
        lock.lock();
        if (into<100) {
            into++;
            System.out.println("编号为 " + into + " 的员工从" + thread.getName() + "入参！拿到的双色球彩票号码是：" + numbers);
        }else {

            System.out.println("人都进入啦");
        }
        lock.unlock();
    }
}